@echo off
chcp 65001 >nul
echo ========================================
echo LimeReader DLL 依赖检查工具
echo ========================================
echo.

set "EXE_PATH=build\bin\Release\LimeReader.exe"

if not exist "%EXE_PATH%" (
    echo [错误] 找不到可执行文件: %EXE_PATH%
    pause
    exit /b 1
)

echo [检查] %EXE_PATH%
echo.

:: 检查是否安装了 Dependencies (替代 Dependency Walker)
set "DEPS_EXE="
if exist "C:\Program Files\Dependencies\Dependencies.exe" (
    set "DEPS_EXE=C:\Program Files\Dependencies\Dependencies.exe"
)
if exist "C:\Program Files (x86)\Dependencies\Dependencies.exe" (
    set "DEPS_EXE=C:\Program Files (x86)\Dependencies\Dependencies.exe"
)

if not "%DEPS_EXE%"=="" (
    echo [工具] 使用 Dependencies 分析依赖...
    echo 正在启动 Dependencies GUI...
    start "" "%DEPS_EXE%" -gui "%EXE_PATH%"
    echo.
    echo Dependencies 已启动，请在 GUI 中查看依赖关系
    pause
    exit /b 0
)

:: 手动检查常见依赖
echo [手动检查] 常见依赖 DLL：
echo ----------------------------------------
echo.

set "RELEASE_DIR=build\bin\Release"
set "MISSING_COUNT=0"

:: Qt 核心库
call :CheckDLL "Qt6Core.dll"
call :CheckDLL "Qt6Gui.dll"
call :CheckDLL "Qt6Widgets.dll"
call :CheckDLL "Qt6Network.dll"
call :CheckDLL "Qt6PrintSupport.dll"

echo.
echo MinGW 运行时:
call :CheckDLL "libgcc_s_seh-1.dll"
call :CheckDLL "libstdc++-6.dll"
call :CheckDLL "libwinpthread-1.dll"

echo.
echo Poppler PDF 库:
call :CheckDLL "libpoppler-153.dll"
call :CheckDLL "libpoppler-cpp-2.dll"

echo.
echo ----------------------------------------
if %MISSING_COUNT%==0 (
    echo [成功] 所有必需的 DLL 都已找到！
) else (
    echo [警告] 缺少 %MISSING_COUNT% 个 DLL 文件
    echo.
    echo 请运行 create_installer.bat 来自动收集所有依赖
)

echo.
echo ========================================
echo 检查 Qt 插件目录：
echo ========================================
echo.

call :CheckPlugin "platforms\qwindows.dll" "必需"
call :CheckPlugin "styles\qwindowsvistastyle.dll" "可选"
call :CheckPlugin "imageformats\qjpeg.dll" "推荐"
call :CheckPlugin "imageformats\qpng.dll" "推荐"

echo.
echo ========================================
echo 注意事项：
echo ========================================
echo 1. 如果缺少 DLL，运行: create_installer.bat
echo 2. 或手动运行 Qt windeployqt 工具
echo 3. 推荐安装 Dependencies 工具查看完整依赖树
echo    下载地址: https://github.com/lucasg/Dependencies
echo ========================================
echo.
pause
exit /b 0

:CheckDLL
if exist "%RELEASE_DIR%\%~1" (
    echo [✓] %~1
    exit /b 0
) else (
    echo [✗] %~1 - 缺失
    set /a MISSING_COUNT+=1
    exit /b 1
)

:CheckPlugin
if exist "%RELEASE_DIR%\%~1" (
    echo [✓] %~1 (%~2)
) else (
    echo [✗] %~1 - 缺失 (%~2)
)
exit /b 0

